#ifndef __udsf_data_sendx_C_
#define __udsf_data_sendx_C_

#include "udf_global.h"

/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "UDSF.c"形式引用
*/

#include "udsf_uart.c"
#include "udsf_spi.c"
#include "udsf_delay.c"

// 最后修改：2023年6月5日 09点32分 V1.0


static void udsf_data_sendx(uint8_t chirp_mode,struct frame_struct *pFrame,int data_len,int total_pkg,int curr_pkg,int chirp_interval) __attribute__((unused));

// chirp_mode 0-行存储 其它-列存储间隔
static void udsf_data_sendx(uint8_t chirp_mode,struct frame_struct *pFrame,int data_len,int total_pkg,int curr_pkg,int chirp_interval)
{	
	struct frame_info * pFrameInfo = &pFrame->frame_info;
	
	pFrameInfo->version = FRAME_INFO_VERSION;
	uint8_t bytes_num = 4;

	if( pFrameInfo->frame_class == eFRAME_TIMING_8BIT ){
		bytes_num = 1;
	}else if( pFrameInfo->frame_class == eFRAME_POINTRST_ABS ){
		bytes_num = 8;
	}
	
	uint32_t chirp_len = pFrameInfo->samp_num * pFrameInfo->samp_ant * bytes_num;
	if( data_len < 0 ){
		pFrameInfo->data_len = chirp_len * pFrameInfo->samp_chirp;
	}else{
		pFrameInfo->data_len = data_len;
	}

	pFrameInfo->pack_curr  = curr_pkg;
	pFrameInfo->pack_total = total_pkg;
	
	if( pFrame->Proto_MVersion > 1){
		uint8_t frame_head = 0xfe;
		pFrame->send_frame((uint8_t*)&frame_head,1);
		pFrame->send_data((uint8_t*)pFrameInfo,sizeof(struct frame_info));
	}else if( pFrame->Proto_MVersion == 1 ){
		uint8_t frame_head[]={1,2,3,4,5,6,7,8};
		pFrame->send_frame(frame_head,sizeof(frame_head));
	}

	if( chirp_mode != 0 ){ // 列模式
			for( int i=0;i<pFrameInfo->samp_chirp;i++){
				for( int j=0;j<pFrameInfo->samp_ant;j++){
					for( int k=0;k<pFrameInfo->samp_num;k++ ){
						pFrame->send_data((uint8_t*)pFrame->pDataSrc +j*pFrame->DataSrc_ChirpInc + k*bytes_num*chirp_mode + bytes_num*i,
						bytes_num);
				}
			}
		}
	}else{
		if( chirp_interval == 0 ){
			pFrame->send_data((uint8_t*)pFrame->pDataSrc,pFrameInfo->data_len);
		}else{
			for( int i=0;i<pFrameInfo->samp_chirp;i++){
				pFrame->send_data((uint8_t*)pFrame->pDataSrc+i*chirp_interval,chirp_len);
			}
		}
	}

	if( pFrame->Proto_MVersion > 1 ){
		uint8_t frame_tail = 0xff;
		pFrame->send_frame((uint8_t*)&frame_tail,1);
	}
	
	if( curr_pkg == total_pkg ){
		pFrame->frame_info.draw_id++;
	}
}


static void udsf_data_send(uint8_t chirp_mode,struct frame_struct *pFrame,int data_len,int total_pkg,int curr_pkg) __attribute__((unused));

// chirp_mode 0-行存储 其它-列存储间隔
static void udsf_data_send(uint8_t chirp_mode,struct frame_struct *pFrame,int data_len,int total_pkg,int curr_pkg)
{	
	struct frame_info * pFrameInfo = &pFrame->frame_info;
	
	pFrameInfo->version = FRAME_INFO_VERSION;
	uint8_t bytes_num = 4;

	if( pFrameInfo->frame_class == eFRAME_TIMING_8BIT ){
		bytes_num = 1;
	}else if( pFrameInfo->frame_class == eFRAME_POINTRST_ABS ){
		bytes_num = 8;
	}
	
	if( data_len < 0 ){
		pFrameInfo->data_len = pFrameInfo->samp_num * pFrameInfo->samp_ant * pFrameInfo->samp_chirp * bytes_num;
	}else{
		pFrameInfo->data_len = data_len;
	}

	pFrameInfo->pack_curr  = curr_pkg;
	pFrameInfo->pack_total = total_pkg;

	if( pFrame->Proto_MVersion > 1){
		uint8_t frame_head = 0xfe;
		pFrame->send_frame((uint8_t*)&frame_head,1);
		pFrame->send_data((uint8_t*)pFrameInfo,sizeof(struct frame_info));
	}else if( pFrame->Proto_MVersion == 1 ){
		uint8_t frame_head[]={1,2,3,4,5,6,7,8};
		pFrame->send_frame(frame_head,sizeof(frame_head));
	}

	if( chirp_mode != 0 ){ // 列模式
			for( int i=0;i<pFrameInfo->samp_chirp;i++){
				for( int j=0;j<pFrameInfo->samp_ant;j++){
					for( int k=0;k<pFrameInfo->samp_num;k++ ){
						pFrame->send_data((uint8_t*)pFrame->pDataSrc +j*pFrame->DataSrc_ChirpInc + k*bytes_num*chirp_mode + bytes_num*i,
						bytes_num);
				}
			}
		}
	}else{
		pFrame->send_data((uint8_t*)pFrame->pDataSrc,pFrameInfo->data_len);
	}

	if( pFrame->Proto_MVersion > 1 ){
		uint8_t frame_tail = 0xff;
		pFrame->send_frame((uint8_t*)&frame_tail,1);
	}
	
	if( curr_pkg == total_pkg ){
		pFrame->frame_info.draw_id++;
	}
}

#endif
